package com.tensquare.base.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tensquare.base.client.GatheringClient;
import com.tensquare.base.service.ILabelService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.security.annotation.PreAuthorize;
import com.tensquare.base.domain.Label;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 标签Controller
 * 
 * @author hbxfwf
 * @date 2021-08-24
 */
@RestController
@RequestMapping("/label")
public class LabelController extends BaseController
{
    @Autowired
    private ILabelService labelService;
    @Autowired
    private GatheringClient gatheringClient;

    //在base模块调用远程的gathering模块
    @GetMapping("/gathering/findId/{id}")
    public  AjaxResult findById(@PathVariable("id") String id){
        return gatheringClient.findById2(id);
    }
    /**
     * 查询标签列表
     */
   // @PreAuthorize(hasPermi = "base:label:list")
    @GetMapping("/list")
    public TableDataInfo list(Label label) throws BlockException {
        startPage();
        //测试熔断限流(演示 @SentinelResourceiy 注解)
         //List<Label> list = labelService.selectLabelList2(label);
        //采用try...catch方式定义资源（演示sentinel）
        //List<Label> list = labelService.selectLabelList(label);
        //演示分布式锁（第一种方式：lua脚本 + Redis实现）
        //List<Label> list = labelService.selectLabelList3(label);
        //演示分布式锁（第二种方式：redisson实现）
        List<Label> list = labelService.selectLabelList4(label);
        return getDataTable(list);
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize(hasPermi = "base:label:export")
    //@Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Label label) throws IOException
    {
        List<Label> list = labelService.selectLabelList(label);
        ExcelUtil<Label> util = new ExcelUtil<Label>(Label.class);
        util.exportExcel(response, list, "标签数据");
    }

    /**
     * 获取标签详细信息
     */
    @PreAuthorize(hasPermi = "base:label:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(labelService.selectLabelById(id));
    }

    /**
     * 新增标签
     */
    @PreAuthorize(hasPermi = "base:label:add")
    //@Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Label label)
    {
        long id = IdUtil.createSnowflake(0, 1).nextId();
        label.setId(id + "");
        return toAjax(labelService.insertLabel(label));
    }

    /**
     * 修改标签
     */
    @PreAuthorize(hasPermi = "base:label:edit")
    //@Log(title = "标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Label label)
    {
        System.out.println("label[update] = " + label);
        return toAjax(labelService.updateLabel(label));
    }

    /**
     * 删除标签
     */
    @PreAuthorize(hasPermi = "base:label:remove")
   // @Log(title = "标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(labelService.deleteLabelByIds(ids));
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody Label label){
        try {
            labelService.save(label);
        } catch (Exception e) {
            return AjaxResult.error("error");
        }
        return AjaxResult.success("ok");
    }


}
